﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Linq;
using Spaanjaars.ContactManager.Infrastructure.Domain;
using Spaanjaars.ContactManager.Infrastructure.UnitOfWork;
using Spaanjaars.ContactManager.Model;

namespace Spaanjaars.ContactManager.Repository.NH
{
    public class ContactPersonRepository : IContactPersonRepository
    {
        private readonly IUnitOfWork _uow;

        public ContactPersonRepository(IUnitOfWork uow)
        {
            _uow = uow;
        }

        public ContactPerson FindBy(int id)
        {
            return SessionFactory.GetCurrentSession().Get<ContactPerson>(id);
        }

        public int Count()
        {
            return SessionFactory.GetCurrentSession().Linq<ContactPerson>().Count();
        }

        public IEnumerable<ContactPerson> FindAll()
        {
            return SessionFactory.GetCurrentSession().CreateCriteria(typeof(ContactPerson)).List<ContactPerson>();
        }

        public IEnumerable<ContactPerson> FindAll(int index, int count)
        {
            return SessionFactory.GetCurrentSession().CreateCriteria(typeof(ContactPerson)).SetFetchSize(count).SetFirstResult(index).List<ContactPerson>();
        }

        public IEnumerable<ContactPerson> QueryWith(ISpecification<ContactPerson> specification)
        {
            return SessionFactory.GetCurrentSession().Linq<ContactPerson>().Where(specification.Predicate).AsQueryable();
        }

        public IEnumerable<ContactPerson> QueryWith(ISpecification<ContactPerson> specification, int index, int count)
        {
            return SessionFactory.GetCurrentSession().Linq<ContactPerson>().Where(specification.Predicate).Skip(index).Take(count).AsQueryable();
        }

        public void Save(ContactPerson entity)
        {
            SessionFactory.GetCurrentSession().SaveOrUpdate(entity);
        }

        public void Add(ContactPerson entity)
        {
            SessionFactory.GetCurrentSession().Save(entity);
        }

        public void Remove(ContactPerson entity)
        {
            SessionFactory.GetCurrentSession().Delete(entity);
        }
    }
}
